﻿Ext.define('app.system.substance.Edit',
{
    extend: 'Ext.window.Window',
    //autoScroll: true,
    modal: true,
    maximized: false,
    width: 860,
    height:500,
    layout: 'fit',
    cdx: '',
    img: '',
    oldImg: '',
    mol: '',
    draftId: 0,
    subId: 0,
    initComponent: function () {
        var me = this;
        me.items = [me.getForm()];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                scrollable: 'y',
                items: [
                {
                    xtype: 'container',
                    layout: 'column',

                    items: [
                        {
                            xtype: 'container',
                            columnWidth: 0.5,
                            items: [
                                this.getMolContainer()
                            ]

                        },
                        {
                            xtype: 'container',
                            columnWidth: 0.5,
                            items: [
                                this.getFormRightContainer()
                            ]

                        }
                    ]

                }
                ]
            });
        }
        return this.form;
    },
    getMolContainer: function () {
        if (!this.molContainer) {
            var me = this;
            this.molContainer = Ext.create("app.ux.GSE.gsePanel", {
                mol:'',
                img: '',
                listeners: {
                    afterCommit: function (mf, mw) {
                        console.log('afterCommit>>mw,mf');
                        var form = me.form.getForm();
                        form.setValues({ molecularWeight: mw, formula: mf });
                    }
                }
            });
            }
        return this.molContainer;
    },
    getFormRightContainer: function () {
        if (!this.formRightContainer) {
            this.formRightContainer = {
                xtype: 'panel', layout: 'form', border: false,
                defaults: { anchor: '100%', xtype: 'textfield', labelWidth: 200, labelAlign: "right" },
                items: [
                    {
                        xtype: 'hidden',
                        name: 'Id',
                        value: 0
                    },
                     {
                         fieldLabel: '中文名',
                         allowBlank: false,
                         name: 'cnName'
                     },
                    {
                        xtype: 'textarea',
                        height: 55,
                        fieldLabel: '英文名',
                        name: 'enName'
                    },
                    {
                        fieldLabel: '分子式',
                        name: 'formula'
                    },
                     {
                         fieldLabel: '分子量',
                         name: 'molecularWeight'
                     },
                     {
                         fieldLabel: 'CAS',
                         name: 'cas'
                     },
                    {
                        xtype: 'textarea',
                        height: 55,
                        fieldLabel: '高风险',
                        name: 'highRisk'
                    },
                    {
                        xtype: 'textarea',
                        height: 55,
                        fieldLabel: '备注',
                        name: 'note'
                    }
                ]
            };
        }
        return this.formRightContainer;
    },
    onSubmit: function () {
        var me = this;
        var formOjbect = me.form.getForm().getValues();
        formOjbect.MOL = me.getMolContainer().mol;
        formOjbect.MolPngBase64 = me.getMolContainer().img;
        formOjbect.CDX = me.getMolContainer().cdx||'';
        formOjbect.Id = me.subId;
        App.AjaxPost('/api/services/app/substance/CreateSubstance', formOjbect, function (data)
        {
            me.fireEvent("savesuccess");
        });
    },
    listeners: {
        'afterrender': function (win) {
            if (win.subId > 0)
            {
                console.log('load sub>>' + this.subId);
                App.AjaxGet("/api/services/app/substance/GetById?id=" + this.subId, function (json) {
                    console.log('load back>>');
                    console.log(json);
                    win.form.getForm().setValues(json.result);
                    win.getMolContainer().mol = json.result.mol;
                    win.getMolContainer().img = json.result.molPngBase64;
                    win.getMolContainer().cdx = json.result.cdx;
                    win.getMolContainer().mf = json.result.formula;
                    win.getMolContainer().mw = json.result.molecularWeight; 
                    win.getMolContainer().showImg();
                })
            }
        }
    }
});